<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <head th:include="commom::title('养老院后台管理系统')"></head>
    <style>
        .layui-table-col-special{

        }
    </style>
</head>

<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="layui-form-item">
                    <div class="layui-inline layui-form-pane">
                        <label class="layui-form-label" >服务名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="proname" name="proname" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" id="ss" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                    </div>
                </div>
            </div>
        </fieldset>


        <!-- 表 格 信 息 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">

        </table>
        <!-- 行 工 具 栏 -->
        <script type="text/html" id="currentTableBar">
            <a  class="layui-btn1 layui-btn-normal layui-btn-xs data-count-edit"
               lay-event="edit"
               style="border: 1px solid white; border-radius: 1px; display: none;">评价</a>
            <a  class="layui-btn2 layui-btn-xs layui-btn-danger data-count-delete"
               lay-event="delete"
               style="border: 1px solid white; border-radius: 1px; display: none;">删除</a>
        </script>
    </div>
</div>
<script th:src="@{/static/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','laydate','layer','laypage'], function () {
        const $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            laydate=layui.laydate,
            table = layui.table;

        //执行一个laydate实例
        // laydate.render({
        //     elem: '#monTime',
        //     type: 'month'
        // });


        table.render({
            elem: '#currentTableId',
            url: '/root/myapply',
            limits: [3, 5, 10, 20],
            limit: 10,
            page: true,
            toolbar: '#toolbarDemo',
            text:"未评价",
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", field: 'center'},
                    {field: 'id', width: 80, title: 'ID', sort: true, align:'center'},
                    {field: 'proname', width: 120, title: '服务名', align:'center'},
                    {field: 'cost', width: 80, title: '价格', align:'center'},
                    {field: 'starttime', width: 165, title: '预约时间', align:'center'},
                    {field: 'user', width: 80, title: '申请人', align:'center'},
                    {field: 'status', width: 220, title: '申请状态', align:'center'},
                    {field: 'conment', width: 200, title: '服务评价', align:'center'},
                    {fixed: 'right', width: 200, align:'center',title: '操作', toolbar: '#currentTableBar'}
            ]
            ],
            done:function (res,curr,count) {
               console.log(res.data);
               let that=this.elem.next();
               res.data.forEach(function (item, index) {
                   console.log(item.status)
                   if (item.status == '同意-待使用完评价本次服务'){
                       let tr1 = that.find("tr[data-index="+index+"] .layui-table-col-special .layui-table-cell .layui-btn1");
                       tr1.css("display","inline-block")
                   }
                   if (item.status == '已完成'){
                       let tr2 = that.find("tr[data-index="+index+"] .layui-table-col-special .layui-table-cell .layui-btn2");
                       tr2.css("display","inline-block")
                   }
               })
            },
            id: 'testReload'
        });
        $('#ss').on('click', function () {
            // 搜索条件
            const proname = $('#proname').val();
            table.reload('testReload', {
                method: 'post'
                , where: {
                    'proname': proname
                }
                , page: {
                    curr: 1
                }
            });
        });



        table.on('toolbar(currentTableFilter)', function (obj) {
            const data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (obj.event === 'add') {  // 监听添加操作
                const index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '380px'],
                    content: '/view/addMon'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作

                var checkStatus = table.checkStatus('testReload');
                var selectCount = checkStatus.data.length;
                if(selectCount == 0){
                    layer.msg('批量删除至少选中一项数据',function(){});
                    return false;
                }
                layer.confirm('真的要删除选中的项吗？',function(index){
                    layer.close(index);
                    // 加载层
                    index = layer.load(1, {shade: [0.1,'#fff']});
                    var listStr="";
                    for(var i=0; i<selectCount; i++){
                        listStr = listStr + "," + checkStatus.data[i].id;
                    }
                    $.ajax({
                        type:'post',
                        data:{"listStr":listStr},
                        url:'/root/batchDelapplyfood',
                        success:function(data){
                            layer.close(index);
                            layer.msg(data.msg);
                            table.reload('testReload');
                        }
                    });
                });
            }
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data)
            if (obj.event === 'edit') {
                // var index = layer.open({
                //     title: '编辑',
                //     type: 2,
                //     shade: 0.2,
                //     maxmin:true,
                //     shadeClose: true,
                //     area: ['600px', '380px'],
                //     content: '/view/userapplyconment?id='+data.id,
                // });
               let conment =  prompt("请输入评论")
                if (conment == null){
                   alert("不能输入为空!")
                }else {
                    $.ajax({
                        url:'/root/addapplyconment?id='+data.id+"&conment="+conment,
                        dataType:'json',
                        type:'post',
                        success:function (data) {
                            obj.del();
                            layer.msg(data.msg,{icon: 1,time: 1000});
                            layer.close(layui.index);
                            table.reload('testReload');
                        }
                    })
                }
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    del(data.id,obj,index);
                })
            }
        });

        function del(id,obj,index){
            $.ajax({
                url:'/root/delapplyuser?id='+id,
                dataType:'json',
                type:'post',
                success:function (data) {
                    obj.del();
                    layer.msg(data.msg,{icon: 1,time: 1000});
                    layer.close(layui.index);
                    table.reload('testReload');
                }
            })
        }

    });
</script>

</body>
</html>
